﻿using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using IntoNails.DataAccess;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.InteractionRequests;
using IntoNails.Models;
using IntoNails.Modules.CustomerManagement.EventAggregators;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.ViewModel;

namespace IntoNails.Modules.CustomerManagement.ViewModels
{
    [Export(typeof(ICustomerWorkspaceViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class CustomerWorkspaceViewModel : NotificationObject, ICustomerWorkspaceViewModel
    {
        #region Fields

        private readonly IEventAggregator _eventAggregator;

        private readonly CustomerRepository _customerRepository;

        private ObservableCollection<Customer> _customerCollection;
        private Customer _selectedCustomer;

        #endregion

        #region Constructors

        [ImportingConstructor]
        public CustomerWorkspaceViewModel(IEventAggregator eventAggregator)
        {
            this._eventAggregator = eventAggregator;

            this._eventAggregator.GetEvent<CreateNewCustomerEventAggregator>().Subscribe(OnCreateNewCustomerEventSubscribed, ThreadOption.PublisherThread);
            this._eventAggregator.GetEvent<EditCustomerEventAggregator>().Subscribe(OnEditCustomerEventSubscribed, ThreadOption.UIThread);
            this._eventAggregator.GetEvent<DeleteCustomerEventAggregator>().Subscribe(OnDeleteCustomerEventSubscribed, ThreadOption.UIThread);

            this.ShowCustomerInteractionDialog = new GenericInteractionRequest<Customer>();
            this.ShowMessageBox = new GenericInteractionRequest<MessageBoxContext>();

            this._customerRepository = new CustomerRepository(GlobalInfoCaches.StartUpPath + @"\Customer.dat");
            this._customerRepository.Load();

            this.PopulateCustomerList();
        }
		
		#endregion

        #region Interaction Requests

        public GenericInteractionRequest<Customer> ShowCustomerInteractionDialog { get; private set; }

        public GenericInteractionRequest<MessageBoxContext> ShowMessageBox { get; private set; }

        #endregion

        #region Properties

        public ObservableCollection<Customer> CustomerCollection
        {
            get { return this._customerCollection; }
            set
            {
                this._customerCollection = value;
                base.RaisePropertyChanged("CustomerCollection");
            }
        }

        public Customer SelectedCustomer
        {
            get { return this._selectedCustomer; }
            set
            {
                this._selectedCustomer = value;
                base.RaisePropertyChanged("SelectedCustomer");
            }
        }

        #endregion

        #region Private Methods

        private void PopulateCustomerList()
        {
            this.CustomerCollection = new ObservableCollection<Customer>(this._customerRepository.GetRawItemList());

        }

        #endregion
    }
}
